//字数限制
$.fn.limitWords = function (max) {
  this.each(function () {
    var text = $.text($(this));
    if (text.length > max) {
      this.innerHTML = text.substring(0, max) + '...';
    }
  });
  return this;
};
//slide淡入淡出轮播
$.fn.fadingSlide = function (options) {
  var settings = $.extend({
      'speed': '3000'
    }, options),
    that = $(this),
    btn = that.find('ul.slide-btn').find('li'),
    imgLi = that.find('ul.slide-img').find('li'),
    ind = 0,
    length = btn.length,
    timer = null;

  function play(ind) {
    btn.removeClass('on').eq(ind).addClass('on');
    imgLi.removeClass('up').eq(ind).addClass('up');
  }

  function witchplay(witch) {
    if (witch > 0) {
      if (ind === length - 1) {
        ind = 0;
        play(ind);
      } else {
        ind++;
        play(ind);
      }
    } else {
      if (ind === 0) {
        ind = length - 1;
        play(ind);
      } else {
        ind--;
        play(ind);
      }
    }
  }

  function autoplay(speed) {
    timer = setInterval(function () {
      witchplay(1);
    }, speed);
  }

  autoplay(settings.speed);
  btn.click(function () {
    ind = $(this).index();
    play(ind);
  });
  that.mouseover(function () {
    clearInterval(timer);
    timer = null;
  }).mouseout(function () {
    autoplay(settings.speed);
  });
  return this;
};
// 选项卡
$.fn.yunTab = function (option) {
  var defaults = {
    tabBar: '.tab-btn',
    tabCon: '.tab-content',
    className: 'this-btn',
    index: 0,
    click:function () {} //这里的this指代按钮DOM
  };
  var options = $.extend(defaults, option);
  var that = $(this);
  that.find(options.tabBar).removeClass(options.className)
    .eq(options.index).addClass(options.className);
  that.find(options.tabCon).hide()
    .eq(options.index).show();
  that.on('click', options.tabBar, function () {
    if (!$(this).hasClass(options.className)) {
      that.find(options.tabBar).removeClass(options.className);
      $(this).addClass(options.className);
      var index = that.find(options.tabBar).index(this);
      that.find(options.tabCon).hide().eq(index).fadeIn(200);
      options.click.apply(this);
    }
  });
};
//无缝滚动
$.fn.rolling = function (options) {
  var settings = $.extend({
    left: true,
    toLeft: true,
    speed: 1
  }, options);
  var that = this, oLi = that.find('li'), d = 0;

  function clearTime(fn) {
    var timer = setInterval(fn, 25);
    that.hover(function () {
      clearInterval(timer);
      timer = null;
    }, function () {
      timer = setInterval(fn, 25);
    });
  }

  if (settings.left) {
    var wi = oLi.length * oLi.outerWidth(true) * 2;
    that.css({width: wi}).html(that.html() + that.html())
      .parent().css({width: wi / 2, overflow: 'hidden'});
    var move = function () {
      if (settings.toLeft) {
        if (-d >= wi / 2) {
          d = 0;
          that.css('left', 0);
        } else {
          d = d - settings.speed;
          that.css('left', d);
        }
      } else {
        that.css('left', -wi / 2);
        if (d >= wi / 2) {
          d = 0;
          that.css('left', -wi / 2);
        } else {
          d = d + settings.speed;
          that.css('left', d - (wi / 2));
        }
      }
    };
    clearTime(move);
  } else {
    var He = oLi.length * oLi.outerHeight(true) * 2;
    that.css({height: He}).html(that.html() + that.html())
      .parent().css({height: He / 2, overflow: 'hidden'});
    var moveTop = function () {
      if (-d >= He / 2) {
        d = 0;
        that.css('top', 0);
      } else {
        d = d - settings.speed;
        that.css('top', d);
      }
    };
    clearTime(moveTop);
  }
  return this;
};
//左右滑动轮播
$.fn.slideBanner = function (options) {
  var setting = $.extend({
      speed: 3000,
      arrow: true,
      hover: true
    }, options),
    that = this,
    bannerUi = that.find('.banner-ul'),
    bannerLi = bannerUi.find('li'),
    arrow = that.find('.banner-arrow'),
    nextBtn = that.find('.next'),
    prevBtn = that.find('.prev'),
    bannerBtn = that.find('.banner-btn').find('li'),
    timer = null,
    state = true,
    index = 0,
    sMove = bannerUi.outerWidth(),
    liNum = bannerLi.length;
  bannerLi.first().css('left', 0);
  function focus() {
    bannerBtn.removeClass('on');
    bannerBtn.eq(index).addClass('on');
  }

  if (!setting.arrow) {
    arrow.css('display', 'none');
  }
  bannerBtn.on('click', function () {
    var thisIndex = $(this).index();
    if (thisIndex > index) {
      bannerLi.eq(index).animate({left: -sMove});
      bannerLi.eq(thisIndex).css({left: sMove}).animate({left: 0});
      index = thisIndex;
      focus();
    } else if (thisIndex < index) {
      bannerLi.eq(index).animate({left: sMove});
      bannerLi.eq(thisIndex).css({left: -sMove}).animate({left: 0});
      index = thisIndex;
      focus();
    }
  });
  function play() {
    bannerLi.eq(index).animate({left: -sMove});
    index++;
    if (index === liNum) {
      index = 0;
      bannerLi.eq(index).css({left: sMove});
    }
    bannerLi.eq(index).css({left: sMove}).animate({left: 0});
    focus();
  }

  function autoPlay() {
    timer = setInterval(play, setting.speed);
  }

  if (setting.hover) {
    that.hover(function () {
      clearInterval(timer);
      timer = null;
    }, function () {
      autoPlay();
    });
  }
  prevBtn.click(function () {
    if (state === true) {
      state = false;
      bannerLi.eq(index).animate({left: sMove});
      index--;
      if (index < 0) {
        index = liNum - 1;
      }
      bannerLi.eq(index).css({left: -sMove}).animate({left: 0}, function () {
        state = true;
      });
      focus();
    }
  });
  nextBtn.click(function () {
    if (state === true) {
      state = false;
      bannerLi.eq(index).animate({left: -sMove});
      index++;
      if (index === liNum) {
        index = 0;
      }
      bannerLi.eq(index).css({left: sMove}).animate({left: 0}, function () {
        state = true;
      });
      focus();
    }
  });
  autoPlay();
  if (liNum === 1) {
    clearInterval(timer);
    timer = null;
  }
  return this;
};
//广告浮动
$.fn.floatAd = function (option) {
  var options = $.extend({
      width: "240",
      height: "150",
      speed: 18
    }, option),
    that = this,
    x = 0,
    y = 0,
    xin = true, yin = true;
  that.css({
    position: 'fixed',
    zIndex: 99999,
    width: options.width,
    height: options.height,
    backgroundColor: '#ffffff'
  });
  $('<span id="close-btn">&times;</span>').css({
    position: 'absolute',
    top: 0,
    right: -20,
    width: 20,
    height: 20,
    lineHeight: '20px',
    fontSize: '18px',
    textAlign: 'center',
    cursor: 'pointer',
    backgroundColor: '#dadada'
  }).appendTo(that);
  var DW = $(window).width(),
    DH = $(window).height(),
    OW = that.width(),
    OH = that.height(),
    L = 0,
    T = 0;

  function float() {
    x = x + (xin ? 1 : -1);
    if (x < L) {
      xin = true;
      x = L;
    }
    if (x > DW - OW - 1) {
      xin = false;
      x = DW - OW - 1;
    }
    y = y + (yin ? 1 : -1);
    if (y > DH - OH - 1) {
      yin = false;
      y = DH - OH - 1;
    }
    if (y < T) {
      yin = true;
      y = T;
    }
    that.css({
      'top': y,
      'left': x
    });
  }

  var timer = setInterval(float, options.speed);
  that.hover(function () {
    clearInterval(timer);
    timer = null;
  }, function () {
    if (that.css('display') !== 'none') {
      timer = setInterval(float, options.speed);
    }
  });
  $('#close-btn').click(function (e) {
    e.stopPropagation();
    that.css('display', 'none');
  });
  return this;
};
//年月选择
$.fn.setYear = function (setYear) {//设置近N年
  var that = this;
  var $month = $('#month');
  var date = new Date();
  var year = date.getFullYear();
  var month = date.getMonth();
  var yearList = '', monthList = '';
  for (var i = year; i > year - setYear; i--) {
    yearList += '<option>' + i + '</option>';
  }
  for (var k = month; k > 0; k--) {
    monthList += '<option>' + k + '</option>';
  }
  that.empty().append(yearList);
  $month.empty().append(monthList);
  that.on('change', function () {
    monthList = '';
    if (parseInt($(this).val()) < year) {
      for (var i = 12; i > 0; i--) {
        monthList += '<option>' + i + '</option>';
      }
      $month.empty().append(monthList);
    } else if (parseInt($(this).val()) === year) {
      for (var k = month; k > 0; k--) {
        monthList += '<option>' + k + '</option>';
      }
      $month.empty().append(monthList);
    }
  });
};